<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>layout 管理系统大布局 - Layui</title>
  <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>
<body>
  <div class="container">
    <!-- 面包屑 -->
    <span class="layui-breadcrumb">
      <a href="/dashBoard">首页</a>
      <a href="/itemLists">商品管理</a>
      <a><cite>增加商品</cite></a>
    </span>
    <!-- /面包屑 -->
    <!-- 增加商品表单 -->
    <form class="layui-form" action="" style="max-width: 800px;margin-top:20px">
      <div class="layui-form-item">
        <label class="layui-form-label">商品名字</label>
        <div class="layui-input-block">
          <input type="text" name="name" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">商品价格</label>
        <div class="layui-input-block">
          <input type="number" name="price" required lay-verify="required" placeholder="请输入商品价格" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品描述</label>
        <div class="layui-input-block">
          <textarea name="desc" placeholder="请输入商品描述" class="layui-textarea"></textarea>
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">封面图片</label>
        <div class="layui-input-block">
          <button type="button" class="layui-btn" id="upload">
            <i class="layui-icon">&#xe67c;</i>上传图片
          </button>
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品详情</label>
        <div class="layui-input-block">
          <div id="content"></div>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="addItem">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>
    <!-- /增加商品表单 -->
  </div>
  <script src="/static/lib/layui/layui.js"></script>
  <script src="/static/lib/wangEditor.js"></script>
  <script>
    //Demo
    layui.use(['form', 'jquery', 'upload','layer'], function () {
      var form = layui.form;
      var $ = layui.jquery;
      var upload = layui.upload; 
      var layer = layui.layer;
      var thumb = '';
      // 初始化文件上传控件
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        ,url: '/upload/' //上传接口
        ,field: 'file'
        ,done: function(res){
          //上传完毕回调
          if(res.code ===0){
            layer.msg(res.msg);
            thumb = res.data;
          }
        }
        ,error: function(){
          //请求异常回调
        }
      });
      // 初始化富文本

      var E = window.wangEditor
      var editor = new E('#content');
      // 调用create方法
      editor.create();
      //提交
      form.on('submit(addItem)', function (data) {
        // 拿到富文本输入的 字符串html内容
        console.log(editor.txt.html());
        // 发送请求 给增加商品路由
        $.ajax({
          url: '/addItem',
          type: 'post',
          data: {
            ...data.field,
            thumb,
            content: editor.txt.html()
          },
          success: function(res){
            // 新增商品成功
            if(res.code === 0) {
              layer.msg(res.msg, {
                icon: 1,
                time: 2000
              }, function(){
                // 回到商品管理
                location.href="/itemLists"
              })
            }
          }
        })
        return false;
      });
    });
  </script>

  </body>

  </html>